<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="ruleForm.userName" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="登录名" prop="username">
      <el-input v-model="ruleForm.account" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="所属部门" prop="department">
      <el-input v-model="ruleForm.orgName" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="是否部门领导" prop="isDepartmentHeads">
      <el-input v-model="ruleleader" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="phone">
      <el-input v-model.number="ruleForm.phone"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="ruleForm.email"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {getInfo, modifyUserInfo} from '@/server/personCenter'

  export default {

    data() {

      var validateTel = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机号不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入正确的手机号'));
          } else {
            if (value.toString().length != '11') {
              callback(new Error('请输入正确的手机号'));
            } else {
              callback();
            }
          }

        }, 10);
      };

      return {
        ruleForm: {},
        ruleleader: '',
        rules: {
          phone: [
            { required: true, validator: validateTel, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱地址', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
          ]
        }
      };
    },
    // props: ['tableData'],
    mounted() {
      this.getBaseInfo();
    },
    methods: {
      getBaseInfo() {
        getInfo().then((res) => {
          if (res.code == 1) {
           let sData = res.data.datas[0];
           this.ruleForm = sData;
           this.ruleleader = this.ruleForm.leader ? '是' : '否';
          } else {
           this.$message(res.message);
          }
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            let Obj = {
              "email": this.ruleForm.email,
              "phone": this.ruleForm.phone
            }
            this.modifyUserInfoFn(Obj);
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      modifyUserInfoFn(paramObj) {
        modifyUserInfo(paramObj).then((res) => {

          if (res.code == '1') {

            this.$notify({
              // title: '恭喜你',
              message: res.message,
              type: 'success',
              duration: 3000
            });
          } else {
           this.$message.error(res.message);
          }
        })
      }
    }
  }
</script>
